package com.example.superoa.controller;

import java.sql.Time;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.mappers.BanCiMapper;
import com.example.superoa.model.pojos.KqAttendance;
import com.example.superoa.model.pojos.KqBanci;
import com.example.superoa.model.service.BanCiService;
import com.example.superoa.model.service.DakaService;
import com.example.superoa.model.service.LeaveService;

@Controller
public class DaKaController {
	@Autowired
	private DakaService daka_biz;
	
	@Autowired
	private BanCiMapper banci_map;
	
	
	 Shijian sj=new Shijian();

	//打卡
	@RequestMapping("kq_daka")
	@ResponseBody
	public String initForm(HttpSession session) {
		Integer dakaid=Integer.parseInt(session.getAttribute("sid").toString());
		JSONObject obj=daka_biz.panduan(dakaid);
		KqAttendance pojos;
		JSONObject banci=banci_map.selectqiyong();
		Date hourqian=sj.getDaiTime2(banci.getDate("b_dao"), 1);
		if(sj.getPanduanShi1(new Date(sj.getNetworkTime()),hourqian)>=0){
			if(!daka_biz.selectPanduan(dakaid, new Date(sj.getNetworkTime()))) {
				return "今天可以休息，无需打卡";
			}else {
				if(obj==null){
					pojos=new KqAttendance();
					pojos.setAShen(1);
					pojos.setABan(1);
					pojos.setAChu(1);
					return daka_biz.Dao(dakaid,new Time(sj.getNetworkTime()),new Date(sj.getNetworkTime()),pojos);
				}else{
					pojos=daka_biz.selectid(Integer.parseInt(obj.get("a_id").toString()));
					try {
						if(!obj.get("a_tui").equals("")) {
							if(daka_biz.Zhunshi(pojos.getKqBanci())==2){
								return daka_biz.Tui(pojos,new Time(sj.getNetworkTime()));
							}else {
								return "2";
							}
						}
						
					} catch (Exception e) {
						// TODO: handle exception
						return daka_biz.Tui(pojos,new Time(sj.getNetworkTime()));
					}
				}
			}
		}else {
			return "1";
		}
		return null;
		
	}
}
